﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace TnaguTetris
{
    /// <summary>
    /// Siia klassi tuleb kõik, mis on seotud menüü ja kasutajaliidesega
    /// </summary>
    class Kasutajaliides
    {

        /// <summary>
        /// Konstandid mida kasutame selle klassi lõikes.
        /// </summary>
        private const String XML_DATA_HOLDER = "options.xml";
        private const String XML_DATA_HOLDER_ELEMENT = "data";

        /// <summary>
        /// muudab optionsis 1'e yes'iks ja 0'i no'ks
        /// </summary>
        /// <param name="value">Algväärtus</param>
        /// <returns>Algväärtuse vastand</returns>
        public string OnOff(int value)
        {
            if (value == 1)
            {
                return "yes";
            }
            return "no";
        }

        /// <summary>
        /// laeb argumendina ette antud seade "node" options.xml failist
        /// </summary>
        /// <param name="option">Options, mida soovid laadida</param>
        /// <returns>Optionsi väärtus</returns>
        public int GetOption(string option)
        {
            String tmp = "";
            XDocument xDoc = XDocument.Load(XML_DATA_HOLDER);
            try
            {
                var data = from x in xDoc.Elements(XML_DATA_HOLDER_ELEMENT) select x;

                foreach (var x in data)
                {
                    tmp = x.Element(option).Value.ToString();

                }
                return int.Parse(tmp);

            }
            catch (Exception)
            {
                return 0;
            }
        }


        /// <summary>
        /// Salvestab argumendina ette antud seade "node" options.xml faili
        /// </summary>
        /// <param name="option">Optrions mida soovid salvestada</param>
        /// <param name="value">Väärtus</param>
        public void SetOption(string option, int value)
        {
            XDocument xDoc = XDocument.Load(XML_DATA_HOLDER);
            xDoc.Elements(XML_DATA_HOLDER_ELEMENT).Elements(option).Remove();
            XElement uus = new XElement(option, value);
            xDoc.Element(XML_DATA_HOLDER_ELEMENT).Add(uus);
            xDoc.Save(XML_DATA_HOLDER);
        }



        /// <summary>
        /// Loob XML faili (vajalik kasutaja seadete hoidmiseks)
        /// </summary>
        public void MakeXMLOptions()
        {
            XElement root = new XElement(XML_DATA_HOLDER_ELEMENT);
            XDocument xDoc = new XDocument(root);
            XDeclaration dek = new XDeclaration("1.0", "utf-8", "yes");
            xDoc.Declaration = dek;
            XElement music = new XElement("music", 1);
            XElement sfx = new XElement("sfx", 1);
            XElement cheatLine = new XElement("cheatLine", 0);
            XElement scoreLimit = new XElement("scoreLimit", 2000);
            XElement startLvl = new XElement("startLvl", 0);
            XElement port = new XElement("port", 1234);
            xDoc.Element(XML_DATA_HOLDER_ELEMENT).Add(music);
            xDoc.Element(XML_DATA_HOLDER_ELEMENT).Add(sfx);
            xDoc.Element(XML_DATA_HOLDER_ELEMENT).Add(cheatLine);
            xDoc.Element(XML_DATA_HOLDER_ELEMENT).Add(scoreLimit);
            xDoc.Element(XML_DATA_HOLDER_ELEMENT).Add(startLvl);
            xDoc.Element(XML_DATA_HOLDER_ELEMENT).Add(port);
            xDoc.Save(XML_DATA_HOLDER);
        }
    }
}
